package com.superdry.activity;

import java.util.ArrayList;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbStrUtil;
import com.ab.util.AbToastUtil;
import com.alibaba.fastjson.JSON;
import com.drysurper.R;
import com.superdry.activity.AddressActivity.AddressInfo;
import com.superdry.json.Address;
import com.superdry.json.Userinfo;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.StringUtils;
import com.superdry.utils.ToastHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class AddAddressActivity extends Activity implements OnClickListener {

	EditText name_et;
	EditText tel_et;
	EditText address_et;
	TextView addressFinished_tv;
	TextView finished;
	View goback;
	ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.aty_add_address);
		initView();
	}

	public void initView() {

		name_et = (EditText) findViewById(R.id.name_et);
		tel_et = (EditText) findViewById(R.id.tel_et);
		address_et = (EditText) findViewById(R.id.address_et);
		goback = findViewById(R.id.goback_add_address);
		finished = (TextView) findViewById(R.id.finished);
		// addressFinished_tv = (TextView) findViewById(R.id.address_finished);
		finished.setOnClickListener(this);
		goback.setOnClickListener(this);
		// addressFinished_tv.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.finished:

			String name = name_et.getText().toString();
			String tel = tel_et.getText().toString();
			String address = address_et.getText().toString();

			// TODO 判断手机号码
			if (AbStrUtil.isEmpty(name) || AbStrUtil.isEmpty(tel) || AbStrUtil.isEmpty(address)) {
				AbToastUtil.showToast(AddAddressActivity.this, "存在未完善的选项！");
				return;
			}
			String regex = "^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[0-9])|(17[0-9]))\\d{8}$";
			if (!StringUtils.matchStr(tel, regex)) {
				ToastHelper.show(AddAddressActivity.this, "请输入正确的手机号码！");
				return;
			}

			AbHttpUtil httpUtil = AbHttpUtil.getInstance(this);
			httpUtil.setTimeout(5000);
			AbRequestParams params = new AbRequestParams();
			Userinfo info = JSON.parseArray(SPHelper.getString(this, SPHelper.USERINFO), Userinfo.class).get(0);
			params.put("userID", info.getUserID() + "");
			params.put("peopleName", name);
			params.put("phoneNumber", tel);
			params.put("addressName", address);

			httpUtil.post(GlobalConst.GET_ADD_ADDRESS, params, new AbStringHttpResponseListener() {

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					dialog = ProgressDialog.show(AddAddressActivity.this, "提交中", "请稍后。。。");
					finished.setClickable(false);
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					dialog.dismiss();
					finished.setClickable(true);
				}

				@Override
				public void onFailure(int statusCode, String content, Throwable error) {
					// TODO Auto-generated method stub
					AbToastUtil.showToast(AddAddressActivity.this, "添加失败");
				}

				@Override
				public void onSuccess(int statusCode, String content) {
					setResult(0);
					finish();
				}
			});

			break;

		case R.id.goback_add_address:

			finish();
		default:
			break;
		}
	}
}
